﻿package com.devilcat.ob{
	import flash.display.MovieClip ;
	import flash.text.TextField ;
	import flash.events.MouseEvent;    
	import com.devilcat.ui.Battleui;
	import com.devilcat.ui.FloatingText;
	import com.devilcat.dt.Itemdt;
	import com.devilcat.dt.Gamedt;
	import com.devilcat.ui.BlankButtonObject;
	import com.devilcat.ui.PokemonInfor;

	public class Itemob extends MovieClip  {
		public var mItem:Itemdt;
		public function Itemob(its:Itemdt) {
			mItem=its;
			PokemonInforPanel.visible=false;
		}
		public function addEventInGame(canCatch:Boolean){
			reText();			
			if(mItem.Type!="Ball"){
				while (PokemonInforPanel.numChildren > 0)
				{
					PokemonInforPanel.removeChildAt(0);
				}
				for(var i:Number=0;i<Gamedt.Player.pokemonList.length;i++){
					var pkmif:PokemonInfor=new PokemonInfor(Gamedt.Player.pokemonList[i]);				
					pkmif.y = 24 * (-i);
					PokemonInforPanel.addChild(pkmif);
						pkmif.addEventItemInBattle(this);
						this.addEventListener(MouseEvent.MOUSE_OVER,showInfor);
						this.addEventListener(MouseEvent.MOUSE_OUT,hideInfor);	
				}
			}else{
				if(canCatch)
				this.addEventListener(MouseEvent.CLICK,clickBall);
			}
			
		}
		public function clickBall(evt:MouseEvent) {
				if (mItem.NumberOf>0) {	
					if(!Gamedt.BattleUI.MyPokemon.UsingItem && Gamedt.BattleUI.TargetPokemon.pokemon.CurrentHP>0 ){
						Gamedt.BattleUI.ThrowBall(mItem.Name);
						Gamedt.BattleUI.MyPokemon.DelayUseItem(5);
						mItem.NumberOf--;	
						Gamedt.updateItem.Update(mItem);				
					}
				}
		}
		public function addEventOutGame(){			
			reText();			
			if(mItem.Type=="AddStat" || mItem.Type=="Potion"){
				while (PokemonInforPanel.numChildren > 0)
				{
					PokemonInforPanel.removeChildAt(0);
				}
				for(var i:Number=0;i<Gamedt.Player.pokemonList.length;i++){
					var pkmif:PokemonInfor=new PokemonInfor(Gamedt.Player.pokemonList[i]);				
					pkmif.y = 24 * i;
					PokemonInforPanel.addChild(pkmif);
						pkmif.addEventItemOutBattle(this);
						this.addEventListener(MouseEvent.MOUSE_OVER,showInfor);
						this.addEventListener(MouseEvent.MOUSE_OUT,hideInfor);	
				}
			}
		}
		public function clickItemOutBattle(evt:MouseEvent) {
			trace("evt.target.bid "+evt.target.bid)
			if (evt.target.bid!=undefined) {
				if (mItem.NumberOf>0) {
					Gamedt.Player.pokemonList[evt.target.bid].UseItem(mItem);
					evt.target.SetContent(Gamedt.Player.pokemonList[evt.target.bid].CurrentHP,Gamedt.Player.pokemonList[evt.target.bid].Name);		
					reText();
				} else {
					addChild(new FloatingText("KHONG CON ITEM",200,0));
				}
			}
		}		
		public function AddEventBuyItem(){		
			this.addEventListener(MouseEvent.CLICK, clickBuyItem);			
		}
		public function clickBuyItem(evt:MouseEvent) {
			if(Gamedt.Player.gold>=mItem.Cost){
				addChild(new FloatingText("+1",100,0));
				Gamedt.updatePlayer.Update("gold",Gamedt.Player.gold-mItem.Cost);
				Gamedt.Player.AddItem(mItem);
			}else{				
				addChild(new FloatingText("BAN KHONG DU PMP",100,0));				
			}
		}

		private function showInfor(e:MouseEvent) {
			PokemonInforPanel.visible=true;
			bgButton.width=200;
			bgButton.height=Gamedt.Player.pokemonList.length*24;			
		}
		private function hideInfor(e:MouseEvent) {
			PokemonInforPanel.visible=false;
			bgButton.width=1;
			bgButton.height=1;
		}
		public function reText(){
			TName.text=mItem.Name;
			TNumber.text=String(mItem.NumberOf);	
		}
		public function buyText(){
			TName.text=mItem.Name;
			TNumber.text=String(mItem.Cost);	
			
		}
	}
}